package com.tiennt.android.callnotes.models.listadapters;

import java.util.ArrayList;

import com.tiennt.android.callnotes.R;
import com.tiennt.android.callnotes.models.contentholders.CallLogItem;
import com.tiennt.android.callnotes.models.viewholders.CallLogViewHolder;
import com.tiennt.android.callnotes.utils.ProjectUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class CallLogAdapter extends ArrayAdapter<CallLogItem> {
	private Context mContext;
	private ArrayList<CallLogItem> mListItems;

	private Bitmap bmpMissedCall;
	private Bitmap bmpIncall;
	private Bitmap bmpOutcall;

	public CallLogAdapter(Context context, int textViewResourceId,
			ArrayList<CallLogItem> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		mContext = context;
		mListItems = objects;

		bmpIncall = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ic_incall);
		bmpMissedCall = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ic_missedcall);
		bmpOutcall = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ic_outcall);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListItems.size();
	}

	@Override
	public CallLogItem getItem(int position) {
		// TODO Auto-generated method stub
		return mListItems.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		CallLogViewHolder viewHolder = null;

		if (v == null) {
			v = LayoutInflater.from(mContext).inflate(R.layout.call_log_row,
					null);
			viewHolder = new CallLogViewHolder(v);
			v.setTag(viewHolder);
		} else {
			viewHolder = (CallLogViewHolder) v.getTag();
		}

		final CallLogItem item = mListItems.get(position);

		if (item != null) {
			if (item.getCallType() == ProjectUtils.CALLTYPE_INCOMING) {
				viewHolder.getImgvCallType().setImageBitmap(bmpIncall);
			} else if (item.getCallType() == ProjectUtils.CALLTYPE_MISSED) {
				viewHolder.getImgvCallType().setImageBitmap(bmpMissedCall);
			} else if (item.getCallType() == ProjectUtils.CALLTYPE_OUTGOING) {
				viewHolder.getImgvCallType().setImageBitmap(bmpOutcall);
			}

			viewHolder.getTvContactName().setText(item.getContactName());
			viewHolder.getTvLong().setText(item.getLong());
			viewHolder.getTvNumber().setText(item.getNumber());
			viewHolder.getTvTime().setText(item.getTime());
		}
		return v;
	}

	public void removeAt(int index) {
		if (index >= mListItems.size()) {
			return;
		}
		mListItems.remove(index);
		notifyDataSetChanged();
	}

}
